/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.google.code.jmarias.client.phases;

import com.google.code.jmarias.client.Entity;
import com.google.code.jmarias.client.Globals;
import com.google.code.jmarias.client.GameLocals;
import com.google.code.jmarias.client.components.DealCardsComponent;
import com.google.code.jmarias.client.components.ImageRenderComponent;
import com.google.code.jmarias.common.Card;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Faze, ktera po prijeti specificke akce rozda karty a ukonci se.
 * 
 * @author bart
 */
public class DealCardsPhase extends AbstractPhase {


    public DealCardsPhase(Globals globals, GameLocals locals, Card [] cards) throws SlickException {
        this.globals = globals;
        this.locals = locals;


        ArrayList<Entity> cardsEntities = new ArrayList<Entity>();
        ArrayList<Vector2f> cardsPositions = new ArrayList<Vector2f>();

        /* 
         * Vytvorim karty do hracovy ruky.
         * Pokud jsou karty skryte, vytvarim karty pro voleni z lidu.
         */
        if(cards[0].getValue() == null) {
            System.out.println("Vytvarim nulove karty pro volbu z lidu. Pocet: " + cards.length);
            cardsEntities = locals.getHidCardsEntities();
            cardsPositions = locals.getPositionsInHand(cards.length, locals.getHandPosition());
            for(int i = 0; i < cards.length; i++) {
                cardsEntities.add(new Entity("nullcard" + i));
                cardsEntities.get(i).AddComponent(new ImageRenderComponent(0, new Image(globals.getIMGPATH() + "karty/0.gif")));
            }
        } else {
            System.out.println("Vytvarim karty. Pocet: " + cards.length);
            cardsEntities = locals.getCardsEntities();
            cardsPositions = locals.getPositionsInHand(cards.length + locals.getCardsEntities().size(), locals.getHandPosition());
            for(int i = 0; i < cards.length; i++) {
                cardsEntities.add(new Entity("card" + cards[i].toOrdinal(), i, cards[i]));
                cardsEntities.get(cardsEntities.size() - 1).AddComponent(new ImageRenderComponent(0, new Image(globals.getIMGPATH() + "karty/" + cards[i].toOrdinal() + ".gif")));
            }
        }


        // Nastaveni defaultnich hodnot pro vsechny karty v ruce
        for(int i = 0; i<cardsEntities.size(); i++) {
            cardsEntities.get(i).setPosition(cardsPositions.get(0));
            cardsEntities.get(i).setScale(locals.getHandScale());
        }

        locals.getHandEntity().AddComponent(new DealCardsComponent(0,cardsPositions, cardsEntities));
    }

    
    @Override
    public void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException {
        locals.getHandEntity().update(gc, null, delta);
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sb, Graphics gr) throws SlickException {
        locals.getHandEntity().render(gc, sb, gr);
    }
    
}
